<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :is-show-before="false">
        <template #after>
          <el-button size="small" type="primary" @click="$router.push('/employees/import')">导入</el-button>
          <el-button size="small" type="primary" @click="exportExcel">导出</el-button>
          <el-button size="small" type="primary" icon="el-icon-plus" @click="addEmployessDialogVisible = true">新增员工</el-button>
        </template>
      </page-tools>

      <!-- 表格 -->
      <!-- 放置表格和分页 -->
      <el-card style="margin: 10px 0;">
        <el-table border :data="employessList">
          <el-table-column label="序号" sortable type="index" />
          <el-table-column label="姓名" sortable prop="username" />
          <el-table-column label="头像" sortable>
            <template slot-scope="{ row }">
              <img style="width:80px;height:80px;" :src="row.staffPhoto" @click="showCode(row.staffPhoto)">
            </template>
          </el-table-column>
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" sortable prop="formOfEmployment" :formatter="formatType" />
          <el-table-column label="部门" sortable prop="departmentName" />
          <el-table-column label="入职时间" sortable>
            <template slot-scope="{ row }">
              {{ row.timeOfEntry | formatTime }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable prop="enableState" />
          <el-table-column label="操作" sortable fixed="right" width="250">
            <template slot-scope="{row}">
              <el-button type="text" size="small" @click="$router.push('/employees/detail/' + row.id)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :total="total"
            :page-size="query.size"
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>

    <!-- 新增员工弹层 -->
    <add-employees :visible.sync="addEmployessDialogVisible" @on-success="loadEmployess" />
    <!-- 头像弹层 -->
    <el-dialog
      title="二维码"
      :visible.sync="qrDialogVisible"
      width="30%"
    >
      <canvas ref="code" />
    </el-dialog>
  </div>
</template>

<script>
import { getDeptsManager } from '@/api/employess.js'
import obj from '@/api/constant/employees.js'
import AddEmployees from './components/AddEmployees.vue'
const { hireType } = obj
import QrCode from 'qrcode'
console.log(QrCode)

export default {
  name: 'Employess',
  components: { AddEmployees },
  data() {
    return {
      query: {
        page: 1,
        size: 5
      },
      total: 0,
      employessList: [],
      addEmployessDialogVisible: false,
      qrDialogVisible: false
    }
  },
  created() {
    this.loadEmployess()
  },

  methods: {
    async loadEmployess() {
      const res = await getDeptsManager(this.query)
      this.total = res.total
      this.employessList = res.rows
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.loadEmployess()
    },
    formatType(row, column, cellValue, index) {
      const findItem = hireType.find(item => item.id === cellValue)
      return findItem ? findItem.value : '聘工信息错误'
    },
    // 导出excel
    exportExcel() {
      import('@/vendor/Export2Excel').then(async excel => {
        const res = await getDeptsManager({
          page: 1,
          size: this.total
        })
        // console.log(res)
        const headers = {
          '姓名': 'username',
          '手机号': 'mobile',
          '入职日期': 'timeOfEntry',
          '聘用形式': 'formOfEmployment',
          '转正日期': 'correctionTime',
          '工号': 'workNumber',
          '部门': 'departmentName'
        }
        const header = Object.keys(headers) // ['手机号'，'姓名'...] = item1
        const data = res.rows.map(item => { // [{mobile:..},{username:...}]
          return header.map(item1 => {
            if (item1 === '聘用形式') {
              const findItem = hireType.find(typeItem => typeItem.id === (item[headers[item1]] - 0))
              return findItem ? findItem.value : '聘用形式错误'
            }
            return item[headers[item1]]
          })
        })
        console.log(data)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          multiHeader: [['姓名', '基本信息', '', '', '', '', '部门']], // 复杂表头
          merges: ['A1:A2', 'B1:F1', 'G1:G2'],
          filename: '员工信息表', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
      })
    },
    showCode(imgUrl) {
      if (!imgUrl) return this.$message.error('该用户未上传头像')
      this.qrDialogVisible = true
      this.$nextTick(_ => {
        QrCode.toCanvas(this.$refs.code, imgUrl)
      })
    }
  }
}
</script>

<style scoped lang='less'>

</style>
